package com.sgzn.sgpay.util;

import java.lang.reflect.Field;

/**
 * 数据转换的工具类
 * <p/>
 * Created by hp on 2014/9/26.
 */
public class ObjectConverterHelper {

    /**
     * @param o        需要设置的参数
     * @param fildName 指定的属性赋值
     * @param value    指定的值
     */
    public static void setValue(Object o, String fildName, Object value) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
    /*    Field field = o.getClass().getDeclaredField(fildName);

        field.setAccessible(true);

        if (field.getType().equals(value.getClass()))
            field.set(o, value);*/

        Class cls = o.getClass();
        while (cls != null) {

            Field[] fields = cls.getDeclaredFields();
            for (Field field : fields) {
                if (field.getName().equals(fildName)) {
                    field.setAccessible(true);
                    if (field.getType().equals(value.getClass())) {
                        field.set(o, value);
                        return;
                    }
                }
            }
            cls = cls.getSuperclass();
        }
    }


    /**
     * 转换指定的类
     *
     * @param source
     * @param targetType
     * @return
     */

    public static Object convert(Object source, Class targetType) {
        try {
            Object target = targetType.newInstance();
            Field[] tFields = targetType.getDeclaredFields();
            for (Field tField : tFields) {
                String fName = tField.getName();
                tField.setAccessible(true);
                Field sField = source.getClass().getDeclaredField(fName);
                sField.setAccessible(true);
                Object value = sField.get(source);
                setValue(target, fName, value);
            }
            return target;
        } catch (Exception e) {
            return null;
        }
    }


    /**
     * 将字符串转换成数字
     *
     * @param source
     * @return
     * @throws Exception
     */
    public static int string2int(String source) throws Exception {

        Integer i = Integer.valueOf(source);
        return i.intValue();

    }


    public static long interval2Long(String interval) {
        long i = 1L;
        for (String c : interval.split("\\*")) {

            i = i * Integer.valueOf(c);
        }
        return i;
    }

    public static long string2Long(String source) throws Exception {

        Long i = Long.valueOf(source);
        return i.longValue();

    }

    /**
     * 将字符串转换为int，“5*60“转换为300
     *
     * @param source
     * @param splic  *
     * @return
     * @throws Exception
     */
    public static int string2int(String source, String splic) throws Exception {


        int result = 0;
        for (String s : source.split("[" + splic + "]")) {
            if (result == 0) {
                result = 1;
            }
            result = result * string2int(s);

        }

        return result;

    }


//    /**
//     * 解析时间。date格式：yyyyMMddHHmmss
//     *
//     * @param date
//     * @return date类型的时间
//     */
//    public static Date parse(String date) throws Exception {
//
//        try {
//            return SimpleDateFormatUtil.parse(date);
//        } catch (Exception e) {
//            throw new Exception("无法解析输入的日期类型，请已匹配格式输出：yyyyMMddHHmmss");
//        }
//
//    }


}
